package com.liuhan.house.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.liuhan.house.dao.HouseMapper;
import com.liuhan.house.pojo.House;
import com.liuhan.house.pojo.User;
import com.liuhan.house.service.HouseService;
import com.liuhan.house.vo.HouseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HouseServiceImpl implements HouseService {
    @Autowired
    private HouseMapper houseMapper;

    /**
     * 查询房屋列表
     *
     * @param orderBy
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<House> getHouseListByKeyWord(String keyword, String orderBy, int type, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize, orderBy);
        List<House> houseList = houseMapper.getHouseListByKeyWord(keyword, type, orderBy);
        List<HouseVo> houseVoList = Lists.newArrayList();
        for (House listHose : houseList) {
            HouseVo houseVo = assembleUserVo(listHose);
            houseVoList.add(houseVo);
        }
        PageInfo pageInfo = new PageInfo(houseList);
        pageInfo.setList(houseVoList);
       return pageInfo;
    }

    /**
     * 根据houseId 查询房屋详细信息
     * @param houseId
     * @return
     */
    @Override
    public House selectByHouseId(Integer houseId) {

        return houseMapper.selectByPrimaryKey(houseId);


    }


    /**
     * 首页新上房源信息展示
     */
    @Override
    public List<House> getNewHouse() {

        return houseMapper.getHouseOrderByUpdateTime();
    }

    /**
     * 添加房屋
     * @param house
     * @return
     */
    @Override
    public int addHouse(House house, User user) {
        House newHouse = new House();
        newHouse.setHouseName(house.getHouseName());
        newHouse.setHouseType(house.getHouseType());
        newHouse.setHouseRemark(house.getHouseRemark());
        newHouse.setHouseAddress(house.getHouseAddress());
        newHouse.setHouseArea(house.getHouseArea());
        newHouse.setHouseImages(house.getHouseImages());
        newHouse.setHouseCommunityId(house.getHouseCommunityId());
        newHouse.setHouseBaths(house.getHouseBaths());
        newHouse.setHouseBeds(house.getHouseBeds());
        newHouse.setHouseAddress(house.getHouseAddress());
        newHouse.setHousePrice(house.getHousePrice());
        newHouse.setUserId(user.getUserId());
        return houseMapper.insertSelective(newHouse);


    }

    /**
     * 根据邮箱获取代理房屋信息
     * @param userId
     * @return
     */
    @Override
    public List<House> getByUserId(Integer userId) {

        return houseMapper.getByUserId(userId);
    }

    /**
     * 删除房屋
     * @param houseId
     * @return
     */
    @Override
    public int deleteById(Integer houseId) {
        return houseMapper.deleteByPrimaryKey(houseId);
    }

    private HouseVo assembleUserVo(House listHose) {
        HouseVo houseVo = new HouseVo();
        houseVo.setUserId(listHose.getUserId());
        houseVo.setHouseId(listHose.getHouseId());
        houseVo.setHouseName(listHose.getHouseName());
        houseVo.setHouseAddress(listHose.getHouseAddress());
        houseVo.setHouseArea(listHose.getHouseArea());
        houseVo.setHouseBaths(listHose.getHouseBaths());
        houseVo.setHouseBeds(listHose.getHouseBeds());
        houseVo.setHousePrice(listHose.getHousePrice());
        houseVo.setHouseRemark(listHose.getHouseRemark());
        String imgs = listHose.getHouseImages();
        String[] img = imgs.split(",");
        houseVo.setHouseImages(img[0]);
        return houseVo;

    }

}
